package reflect;

import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        p.sayHello();
        String name = p.getName();
        System.out.println("name:"+name);

        Class<?> cls = Class.forName("reflect.Person");
        Object obj = cls.newInstance();

        Method method = cls.getMethod("sayHello");
        method.invoke(obj);

        Method method2 = cls.getMethod("setName",String.class);
        method2.invoke(obj,"张三");

        Method method1 = cls.getMethod("getName");
        Object returnValue = method1.invoke(obj);
        System.out.println(returnValue);

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入类名:");
        String className = scanner.nextLine();

        System.out.println("请输入方法名:");
        String methodName = scanner.nextLine();

        Class<?> cls2 = Class.forName(className);
        Object o = cls2.newInstance();

        Method method4 =cls2.getMethod(methodName);
        System.out.println(method4.invoke(o));
    }
}
